package com.sunnada.edu.business.group.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.group.service.GroupMethods;
import com.sunnada.edu.business.group.service.GroupService;
import com.sunnada.edu.business.project.service.ProjectMethods;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;

/***
 * explain 班组控制层
 * 
 * @author chs 2015-5-12 version 1.0
 */
@Controller
@RequestMapping("/groupController")
public class GroupController extends BasicController {
	@Resource
	GroupService groupService;

	/** 班组首页 */
	@RequestMapping("/group")
	public String group(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "business/group/group";
	}

	/** 班组查询 */
	@RequestMapping("/groupSearch")
	public void groupSearch(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Page page = new Page();
		page.setRowCount(groupService.getGroupCots(params));
		page.setPageMap(request, page, params);
		List<?> groupList = groupService.findGroups(params);
		JSONObject json = new JSONObject();
		json.put("list", groupList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 班组添加页面 */
	@RequestMapping("/groupAdd")
	public String groupAdd(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		return "business/group/group_add";
	}

	/** 班组修改页面 */
	@RequestMapping("/groupUpdate")
	public String groupUpdate(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Map group = groupService.getGroupByRecId(params);
		request.setAttribute("group", group);
		return "business/group/group_update";
	}

	/** 班组详情页面 */
	@RequestMapping("/groupDetail")
	public String groupDetail(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Map group = groupService.getGroupByRecId(params);
		request.setAttribute("group", group);
		return "business/group/group_detail";
	}

	/** 班组保存功能 */
	@RequestMapping("/groupSave")
	public void groupSave(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		String recId = params.get("recId");
		if (Utils.isNotNull(recId)) {
			msg = groupService.groupUpdate(params);
			//废除原来的班长为普通工人
			GroupMethods.setWorker(params);
		} else {
			msg = groupService.groupAdd(params);
		}
		printInfo(response, msg);
	}

	/** 班组删除功能 */
	@RequestMapping("/groupDelete")
	public void groupDelete(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = groupService.groupDelete(params);
		printInfo(response, msg);
	}

	/** 班组停用 */
	@RequestMapping("/groupDisable")
	public void groupDisable(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = groupService.groupDisable(params);
		printInfo(response, msg);
	}

	/** 班组启用 */
	@RequestMapping("/groupEnable")
	public void groupEnable(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = groupService.groupEnable(params);
		printInfo(response, msg);
	}

	/** 班组导出 */
	@RequestMapping("/exportGroupWorkers")
	public void exportGroupWorkers(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		try {
			groupService.exportGroupWorkers(params, request, response);
		} catch (Exception e) {
			printInfo(response,
					"<script>alert('导出失败，请稍后重试！');window.history.go(-1);</script>");
		}
	}

	/** 班组信息导入记录界面 */
	@RequestMapping("/groupImportpage")
	public String groupImportpage(HttpServletRequest request) {
		return "business/group/group_importpage";
	}
	
	
	@RequestMapping("/selectWorkerPage")
	public String selectWorkerPage(HttpServletRequest request) {
		return "business/group/selectWorker";
	}
	
	

	@RequestMapping("/selectMonitorPage")
	public String selectMonitorPage(HttpServletRequest request) {
		return "business/group/selectMonitor";
	}

	@RequestMapping("/selectProjectPage")
	public String selectProjectPage(HttpServletRequest request) {
		return "business/group/selectProject";
	}


	@RequestMapping("/selectWorker")
	public void selectWorker(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Page page = new Page();
		String condition = " AND  T.REC_ID NOT IN(SELECT G.MONITOR_ID FROM t_worker_group g WHERE g.REC_STATUS!='2' ) ";//没当班长
		
		String workerType=params.get("workerType");
		if(Utils.isNotNull(workerType)){
			condition+=" and T.WORKTYPE LIKE '%"+workerType+"%'";
		}
		params.put("condition", condition);
		params.put("SUNNADA_PAGE_SIZE",
				request.getParameter("SUNNADA_PAGE_SIZE"));
		page.setRowCount(groupService.getWorkerCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = groupService.findWorkers(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}
	
	
	@RequestMapping("/getGroupWorkerList")
	public void getGroupWorkerList(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		String groupId = request.getParameter("groupId");
		String workerIds = request.getParameter("workerIds");
		Page page = new Page();
		String condition = "";
		if (Utils.isNotNull(groupId)) {
			condition += " AND T.GROUPID ='"+groupId+"'";
		}
		if (Utils.isNotNull(workerIds)) {
			condition += " AND T.REC_ID IN("+Utils.getSqlSplitStr(workerIds)+")";
		}
		
		params.put("condition", condition);
		
		params.put("SUNNADA_PAGE_SIZE",
				request.getParameter("SUNNADA_PAGE_SIZE"));
		page.setRowCount(groupService.getWorkerListCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = groupService.getWorkerList(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}
	
	/** 班组删除功能 */
	@RequestMapping("/workerRemoveFromGroup")
	public void workerRemoveFromGroup(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		msg = GroupMethods.workerRemoveFromGroup(params)==0?"移除失败！":"";
		printInfo(response, msg);
	}
	
	/** 班组删除功能 */
	@RequestMapping("/delProjectFromGroup")
	public void delProjectFromGroup(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		msg = GroupMethods.delProjectFromGroup(params)==0?"移除失败！":"";
		printInfo(response, msg);
	}
	
	/** 获取所有的项目 */
	@RequestMapping("/getProjectList")
	public void getProjectList(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		List<?> groupList = ProjectMethods.getProjectList(params);
		JSONObject json = new JSONObject();
		json.put("list", groupList);
		json.put("params", params);
		printInfo(response, json);
	}
	
	
	/** 获取班组的项目 */
	@RequestMapping("/getGroupProjectList")
	public void getGroupProjectList(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		List<?> groupList = GroupMethods.getGroupPorjectList(params);
		JSONObject json = new JSONObject();
		json.put("list", groupList);
		json.put("params", params);
		printInfo(response, json);
	}
	
	/** 班组下拉列表 */
	@RequestMapping("/selectGroups")
	public void selectGroups(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		List<?> groupList = GroupMethods.selectGroups(params);
		JSONObject json = new JSONObject();
		json.put("list", groupList);
		json.put("params", params);
		printInfo(response, json);
	}

}
